package com.sure.minion.services.base.vo;

import java.util.List;


/**
 * @Description JPQL查询辅助类
 * @author syuan
 * @date 2015年8月28日  上午11:37:28
 */
public class QueryAssist {

	/** JPQL*/
	private String queryString;

	/** 参数[]*/
	private Object[] queryParas;

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public Object[] getQueryParas() {
		return queryParas;
	}

	public void setQueryParas(Object[] queryParas) {
		this.queryParas = queryParas;
	}

	/**
	 * @Description 拼装and查询关系
	 * @param jpql 
	 * @param paraList 参数List
	 * @param name PO中属性名
	 * @param relation 关系 eg: in igin like iglike igequal = <> <
	 * @param value
	 * @author sure
	 */
	@SuppressWarnings("unchecked")
	public static void buildRelation(StringBuffer jpql, List<Object> paraList, 
			String name, String relation, Object value) {
		
		//如果参数不为空才拼接字符串
		if (null != value && !"".equals(value.toString())) {
			
			// 特殊处理in关系
			if ("in".equalsIgnoreCase(relation)) {
				// 只接受List型参数
				if (value instanceof List) {
					List<Object> valueList = (List<Object>) value;
					if (valueList.size() > 0) {
						jpql.append(" and " + name + " in (?");
						paraList.add(valueList.get(0));

						for (int i = 1; i < valueList.size(); i++) {
							if (i % 1000 == 0) { // in的条件个数最大值为1000
								jpql.append(") or " + name + " in (?");
								paraList.add(valueList.get(i));
							} else {
								jpql.append(",?");
								paraList.add(valueList.get(i));
							}
						}

						jpql.append(")");
					}
				}
			}
			// 特殊处理Igin关系
			else if ("Igin".equalsIgnoreCase(relation)) {
				// 只接受List<String>型参数
				if (value instanceof List) {
					List<String> valueList = (List<String>) value;
					if (valueList.size() > 0) {
						jpql.append(" and UPPER(" + name + ") in (?");
						paraList.add(valueList.get(0).toUpperCase());

						for (int i = 1; i < valueList.size(); i++) {
							if (i % 1000 == 0) { // in的条件个数最大值为1000
								jpql.append(") or UPPER(" + name + ") in (?");
								paraList.add(valueList.get(i).toUpperCase());
							} else {
								jpql.append(",?");
								paraList.add(valueList.get(i).toUpperCase());
							}
						}

						jpql.append(")");
					}
				}
			}
			//特殊处理like关系
			else if ("like".equalsIgnoreCase(relation)) {
				
				jpql.append(" and instr(" + name + " ,(?),-1,1)>0");
				paraList.add(value);
				
			//特殊处理忽略大小写的like关系	
			} else if ("iglike".equalsIgnoreCase(relation)) {
					
				jpql.append(" and instr(UPPER(" + name + ") ,(?),-1,1)>0");
				paraList.add(value.toString().toUpperCase());
			
			//特殊处理忽略大小写的相等关系	
			} else if ("igequal".equalsIgnoreCase(relation)) {
					
				jpql.append(" and UPPER(" + name + ") = (?)");
				paraList.add(value.toString().toUpperCase());
			//普通关系
			} else {
					
				jpql.append(" and " + name + " "+ relation + " (?)");
				paraList.add(value);
			}
		}
				

	}
	
	/**
	 * @Description 拼装查询or关系
	 * @param jpql
	 * @param paraList
	 * @param name
	 * @param relation
	 * @param value
	 * @author sure
	 */
	@SuppressWarnings("unchecked")
	public static void buildOrRelation(StringBuffer jpql, List<Object> paraList, String name, String relation,
			Object value) {
	
		//如果参数不为空才拼接字符串
		if (null != value && !"".equals(value.toString())) {
			
			if (value instanceof List) {
				List<Object> valueList = (List<Object>) value;
				if (valueList.size() > 0) {
					jpql.append(" and (instr(" + name + " ,(?),-1,1)>0");
					if (valueList.get(0) instanceof String) {
						String str = (String) valueList.get(0);
						paraList.add(str.toUpperCase());
					} else {
						paraList.add(valueList.get(0));
					}
					for (int i = 1; i < valueList.size(); i++) {
						jpql.append(" or instr(" + name + " ,(?),-1,1)>0");
						if (valueList.get(i) instanceof String) {
							String str = (String) valueList.get(i);
							paraList.add(str.toUpperCase());
						} else {
							paraList.add(valueList.get(i));
						}
					}
					jpql.append(")");
				}
			}
			
		}
	}


	/**
	 * @Description 拼装orderby
	 * @param jpql
	 * @param orderBy
	 * @author sure
	 */
	public static void buildOrderBy(StringBuffer jpql, String orderBy) {
		// 如果参数不为空才拼接字符串
		if (null !=orderBy  && !orderBy.trim().equals("")) {
			jpql.append(" order by " + orderBy);
		}
	}
	
}
